package com.hulk.dryad.manage.framework.lock.config;


import com.hulk.dryad.manage.framework.lock.executor.LockdExecutor;
import com.hulk.dryad.manage.framework.lock.executor.RedissonLockdExecutor;
import com.hulk.dryad.manage.framework.lock.aop.LockdAnnotationAdvisor;
import com.hulk.dryad.manage.framework.lock.aop.LockdInterceptor;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.SneakyThrows;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分布式锁自动配置器
 *
 * @author hulk
 */
@Configuration
@ConditionalOnClass(RedissonClient.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
@EnableConfigurationProperties(RedisProperties.class)
public class LockdAutoConfiguration {

	private static final String CODEC = "org.redisson.codec.JsonJacksonCodec";

	@SneakyThrows
	@Bean
	@ConditionalOnMissingBean
	public	RedissonClient redisson(RedisProperties properties)  {

		Duration timeout = properties.getTimeout();
		Duration connectTimeout = properties.getConnectTimeout();
		Config config = new Config();
		if(properties.getCluster()!=null){
			List<String> nodes = properties.getCluster().getNodes().stream().map(v->"redis://"+v).collect(Collectors.toList());
			ClusterServersConfig clusterConfig = config.useClusterServers().setPassword(properties.getPassword())
					.addNodeAddress(nodes.toArray(new String[0]));
			if(timeout!=null){
				clusterConfig.setTimeout((int)timeout.toMillis());
			}
			if(connectTimeout!=null){
				clusterConfig.setConnectTimeout((int)connectTimeout.toMillis());
			}

		}else {
			SingleServerConfig singleConfig = config.useSingleServer().setAddress("redis://"+properties.getHost()+":"+properties.getPort())
					.setDatabase(properties.getDatabase())
					.setPassword(properties.getPassword());
			if(timeout!=null){
				singleConfig.setTimeout((int)timeout.toMillis());
			}
			if(connectTimeout!=null){
				singleConfig.setConnectTimeout((int)connectTimeout.toMillis());
			}
		}
		Codec codec=(Codec) ClassUtils.forName(CODEC,ClassUtils.getDefaultClassLoader()).newInstance();
		config.setCodec(codec);
		config.setEventLoopGroup(new NioEventLoopGroup());
		return Redisson.create(config);
	}

    @Bean
	@ConditionalOnMissingBean
    public LockdExecutor redissonLockdExecutor(RedissonClient redisson) {
        return new RedissonLockdExecutor(redisson);
    }
	@Bean
	@ConditionalOnMissingBean
	public LockdInterceptor lockdInterceptor(LockdExecutor redissonLockdExecutor) {
		return new LockdInterceptor(redissonLockdExecutor);
	}
    @Bean
    @ConditionalOnMissingBean
    public LockdAnnotationAdvisor lockdAnnotationAdvisor(LockdInterceptor lockdInterceptor) {
        return new LockdAnnotationAdvisor(lockdInterceptor);
    }
}
